package com.example.mq.domo;

import com.example.mq.common.Consumer;
import com.example.mq.common.MqException;
import com.example.mq.mqchile.Channel;
import com.example.mq.mqchile.Connection;
import com.example.mq.mqchile.ConnectionFactory;
import com.example.mq.mqserver.core.BasicProperties;
import com.example.mq.mqserver.core.ExchangeType;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2024/03/09/12:20
 * @Description: 这个类表示一个消费者  通常这个类也应该是在一个独立的服务器上执行
 */
public class DomoConsumer {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        System.out.println("启动消费者");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPort(9090);
        factory.setHost("127.0.0.1");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("testQueue",true,false,false,null);

        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws MqException, IOException {
                System.out.println("[消费数据  开始]");
                System.out.println("consumerTag = " + consumerTag);
                System.out.println("basicProperties = " + basicProperties);
                String bodyString = new String(body,0, body.length);
                System.out.println("body = " +bodyString);
                System.out.println("[消费数据  结束]");
            }
        });
        while(true) {
            //由于消费者也不知道生产者要生产多少,这个循环模拟一直等待消费
            Thread.sleep(500);
        }
    }
}
